<template>
  <div class="app-container">

        <el-row :gutter="10" class="mb8">
          <el-card shadow="always">
            我的渠道：{{channelName}}
          </el-card>
        </el-row>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >申请二级渠道</el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="channelList" >
          <el-table-column label="二级渠道名称" align="center" prop="name" width="120" :show-overflow-tooltip="true"/>
          <el-table-column label="渠道编号" align="center" prop="channelId" width="80"/>
          <el-table-column label="渠道下载地址" align="center" prop="channelLink" :show-overflow-tooltip="true"/>
          <el-table-column label="联系人" align="center" prop="contactPerson" />
          <el-table-column label="联系电话" align="center" prop="contactPhone" />
          <el-table-column label="微信号" align="center" prop="wechat" />
          <el-table-column label="支付宝名字" align="center" prop="aliName" />
          <el-table-column label="支付宝账号" align="center" prop="aliAccount" />
          <el-table-column label="状态" align="center" >
            <template slot-scope="scope">
              <span v-if="scope.row.applyStatus===1">待审</span>
              <span v-else-if="scope.row.applyStatus===2">已通过</span>
              <span v-else-if="scope.row.applyStatus===3">未通过</span>
              <span v-else-if="scope.row.applyStatus===-1">禁用</span>
              <span v-else>删除</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

      <!-- 添加或修改渠道管理对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="归属一级渠道" >
            {{channelName}}
          </el-form-item>
          <el-form-item label="二级渠道名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入二级渠道名称" />
          </el-form-item>
          <el-form-item label="选择渠道包" prop="channelId">
            <el-select v-model="form.channelId" placeholder="请选择">
              <el-option
                v-for="item in channelPackageOptions" :key="item.id" :label="item.downLink" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="联系人" prop="contactPerson">
            <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
          </el-form-item>
          <el-form-item label="联系方式" prop="contactPhone">
            <el-input v-model="form.contactPhone" placeholder="请输入联系方式" />
          </el-form-item>
          <el-form-item label="微信号" prop="wechat">
            <el-input v-model="form.wechat" placeholder="请输入微信号" />
          </el-form-item>
          <el-form-item label="选择后台帐号" prop="sysUserId">
            <el-select v-model="form.sysUserId" placeholder="请选择">
              <el-option
                v-for="item in userOptions" :key="item.userId" :label="item.nickName" :value="item.userId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="支付宝名字" prop="aliName">
            <el-input v-model="form.aliName" placeholder="请输入支付宝名字" />
          </el-form-item>
          <el-form-item label="支付宝账号" prop="aliAccount">
            <el-input v-model="form.aliAccount" placeholder="请输入支付宝账号" />
          </el-form-item>
          <el-form-item label="身份证号" prop="proveNumber">
            <el-input v-model="form.proveNumber" placeholder="请输入身份证号" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

  </div>
</template>

<script>
import { listChannel, getChannel, delChannel, addChannel, updateChannel, exportChannel } from "@/api/system/channel";
import { loadChannelPackage } from "@/api/system/channelPackage";
import { loadUser } from "@/api/system/user";


export default {
  data() {
    return {
      userOptions:[],
      channelPackageOptions:[],
      oneOpen:false,
      channelName:'',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 渠道管理表格数据
      channelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        channelNumber: [
          { required: true, message: "渠道编号不能为空", trigger: "blur" }
        ],
        parentBusiness: [
          { required: true, message: "归属商务不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "渠道名称不能为空", trigger: "blur" }
        ],
        contactPerson: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        contactPhone: [
          { required: true, message: "联系方式不能为空", trigger: "blur" }
        ],
        wechat: [
          { required: true, message: "微信号不能为空", trigger: "blur" }
        ],
        managerAccount: [
          { required: true, message: "后台账号不能为空", trigger: "blur" }
        ],
        managerPwd: [
          { required: true, message: "后台账号密码不能为空", trigger: "blur" }
        ],
        aliName: [
          { required: true, message: "支付宝名字不能为空", trigger: "blur" }
        ],
        aliAccount: [
          { required: true, message: "支付宝账号不能为空", trigger: "blur" }
        ],
        proveNumber: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        settle: [
          { required: true, message: "结算方式不能为空", trigger: "blur" }
        ],
        maxRegister: [
          { required: true, message: "总注册量限制不能为空", trigger: "blur" }
        ],
        createDate: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    console.log('state = %o',this.$store.state.user)
    loadChannelPackage().then(response => {
      this.channelPackageOptions = response.data;
    });
    loadUser().then(response => {
      this.userOptions = response.data;
    });
    if(this.$store.state.user.channel){
      this.queryParams.parentId = this.$store.state.user.channel.id
      this.channelName = this.$store.state.user.channel.name
    }else{
      this.msgError("您没有渠道账户，不应有此权限！");
      this.$router.go(-1);
      return
    }

    this.getList() 
  },
  methods: {
    /** 查询渠道管理列表 */
    getList() {
      this.loading = true;
      listChannel(this.queryParams).then(response => {
        this.channelList = response.rows;
        this.total = parseInt(response.total);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        channelNumber: undefined,
        parentBusiness: undefined,
        name: undefined,
        contactPerson: undefined,
        contactPhone: undefined,
        wechat: undefined,
        managerAccount: undefined,
        managerPwd: undefined,
        aliName: undefined,
        aliAccount: undefined,
        proveNumber: undefined,
        settle: undefined,
        settleUnitPrice: undefined,
        chatroomCommissionRatio: undefined,
        otherCommissionRatio: undefined,
        maxRegister: undefined,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "申请二级渠道";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getChannel(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改二级渠道";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.form.parentId = this.queryParams.parentId
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateChannel(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addChannel(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除渠道管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delChannel(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    }
  }
};
</script>